﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using web.Models;
using web.BLL;

namespace web.Controllers
{
    public class ManagerController : Controller
    {
        //
        // GET: /Manager/


        public ActionResult Login()
        {
            return View();
        }

        public JsonResult checkLogin() {
            if ((this.Session["CheckCode"] != null) && (this.Session["CheckCode"].ToString() != ""))
            {
                if (this.Session["CheckCode"].ToString().ToLower() != Request["yzm"].ToString().ToLower())
                {
                    return Json("{\"success\":false,\"msg\":\"验证码错误\"}");
                }
                this.Session["CheckCode"] = null;
            }
            string userName = Request["loginname"].ToString();
            string password = Request["password"].ToString();
            userinfo user = sitebll.getUser(userName, password);
            if (user != null)
            {
                Session["User"] = user;
                sitebll.addlog(Request.ServerVariables["Http_User_Agent"]);
 
                

                return Json("{\"success\":true,\"msg\":\"登陆成功\"}");
            }
            else
            {
                return Json("{\"success\":false,\"msg\":\"用户名密码错误\"}");
            }
        }


        public ActionResult Index()
        {
            if (Session["user"] == null)
            {
                return Redirect("/Manager/Login");

            }
            else {
                ViewBag.username = ((userinfo)Session["user"]).username;
                ViewBag.userid = ((userinfo)Session["user"]).id;
            }
            return View();
        }

        public RedirectResult logout() {
           Session.RemoveAll();
           return Redirect("/Manager/login");
        }

    }
}
